package com.qing.administer.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.administer.annotations.ResponseResult;
import com.qing.administer.annotations.WebLog;
import com.qing.administer.entity.SysRole;
import com.qing.administer.entity.SysUser;
import com.qing.administer.pojo.dto.SysUserDTO;
import com.qing.administer.pojo.dto.UserLoginDTO;
import com.qing.administer.pojo.vo.SysUserVO;
import com.qing.administer.service.SysRoleService;
import com.qing.administer.service.SysUserRoleService;
import com.qing.administer.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author qing
 * @since 2021-09-17
 */
@Api(tags = "用户管理")
@ResponseResult
@RestController
@RequestMapping("/administer/sys-user")
public class SysUserController {


	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysUserRoleService sysUserRoleService;


	@WebLog("列表")
	@ApiOperation(value = "列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "current",value = "当前页",required = true,defaultValue = "1"),
			@ApiImplicitParam(name = "size",value = "每页显示条数",required = true,defaultValue = "10"),
			@ApiImplicitParam(name = "status",value = "是否启用：1：启用 0：禁用",required = false),
			@ApiImplicitParam(name = "keyword",value = "模糊匹配",required = false)
	})
	@GetMapping("/page")
	@SaCheckRole("admin")
	public IPage<SysUser> getPage(@RequestParam(value = "current",defaultValue = "1",required = false) Long current,
								  @RequestParam(value = "size",defaultValue = "10",required = false) Long size,
								  @RequestParam(value = "status",defaultValue = "",required = false) Integer status,
								  @RequestParam(value = "keyword",defaultValue = "",required = false) String keyword){

		return sysUserService.findPage(current,size,status,keyword);
	}


	@WebLog("编辑")
	@ApiOperation(value = "编辑")
	@PostMapping
//	@SaCheckPermission("user-edit")
	public Boolean edit(@RequestBody SysUserDTO sysUserDTO){
		SysUser sysUser=new SysUser();
		BeanUtils.copyProperties(sysUserDTO,sysUser);
		sysUserService.saveOrUpdate(sysUser);
		return sysUserRoleService.editSysUserRole(sysUser.getUserId(),sysUserDTO.getRoleIds());
	}


	@WebLog("删除")
	@ApiOperation(value = "删除")
	@ApiImplicitParam(name = "id",value = "用户Id",required = true)
	@DeleteMapping
	@SaCheckRole("super-admin")
	public Boolean delete(@RequestParam(value = "id") Long id){
		return sysUserService.removeById(id);
	}


	@WebLog("详情")
	@ApiOperation(value = "详情")
	@ApiImplicitParam(name = "userId",value = "用户Id",required = true)
	@GetMapping("/detail")
	public SysUserVO getDetail(@RequestParam(value = "userId") Long userId){
		SysUserVO sysUserVO=new SysUserVO();
		BeanUtils.copyProperties(sysUserService.getById(userId),sysUserVO);
		sysUserVO.setRoleList(sysRoleService.selectSysUserRole(userId));
		return sysUserVO;
	}


	/**
	 * 登录
	 * @param userLoginDTO
	 * @return
	 */
	@WebLog("用户登录")
	@ApiOperation(value = "登录")
	@PostMapping("login")
	public String login(@RequestBody UserLoginDTO userLoginDTO) {
		return sysUserService.login(userLoginDTO);
	}

	/**
	 * 退出
	 * @return
	 */
	@WebLog("退出")
	@ApiOperation(value = "退出")
	@GetMapping("logout")
	public Boolean logout() {
		StpUtil.logout();
		return true;
	}


	@WebLog("获取token")
	@ApiOperation(value = "获取token")
	@GetMapping("token")
	public Map<String,Object> getToken() {
		Map<String,Object> map=new HashMap<>();
		map.put("TokenName",StpUtil.getTokenName());
		map.put("TokenValue",StpUtil.getTokenValue());
		map.put("TokenInfo",StpUtil.getTokenInfo());
		return map;
	}
}

